<template>
  <div>
    <el-carousel class="banner">
      <el-carousel-item v-for="(item, index) in banner" :key="index" height="460px;">
        <img :src="item.picUrl" @click="handleBanner(item)"/>
      </el-carousel-item>
    </el-carousel>
    <div class="main">
      <div class="main-left">
        <div class="title">
          <div class="title-left">推荐课程</div>
          <!-- <div class="title-right" @click="$router.push('lessonList')">查看更多</div> -->
        </div>
        <div class="content">
          <div class="item" v-for="(item, index) in recommendLesson" :key="index" @click="toCourse()">
            <img :src="item.picUrl" alt />
            <div class="text">
              <p class="text-p1">{{item.resourceLessonName}}</p>
              <p class="text-p2">
                共{{item.chapterNumber}}章节
                <span>{{item.studyPeopleNum}}人正在学习</span>
              </p>
            </div>
          </div>
        </div>
      </div>
      <div class="main-right">
        <div class="title">
          <div class="title-left">行业新闻</div>
          <!-- <div class="title-right">查看更多</div> -->
        </div>
        <ul>
          <li v-for="(item, index) in news" :key="index" @click="showDetail(item)">
            <div class="name">
              {{item.type}}
              <span>|</span>
            </div>
            <div class="text two-ellipsis">
              {{item.title}}
              <span class="time">({{item.createdTime.slice(5,10)}})</span>
            </div>
          </li>
        </ul>
      </div>
    </div>
    <el-dialog title="新闻详情" :visible.sync="dialogVisible">
      <div class="news-box">
        <div class="title">
          {{newInfo.title}}
        </div>
        <div class="news-desc">
          <el-tag>{{newInfo.type}}</el-tag>
          <div class="news-time">
            {{newInfo.createdTime}}
          </div>
        </div>
        <div class="news-content" v-html="newInfo.content">
        </div>
        <img :src="newInfo.picUrl" v-if="newInfo.picUrl" alt="">
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { main as studentMain } from '@/api/student/index.js'
import { main as teacherMain } from '@/api/teacher/index.js'
import { mapGetters } from 'vuex'

export default {
  name: 'Index',
  computed: {
    ...mapGetters(['userInfo'])
  },
  data () {
    return {
      value2: 88,
      banner: [],
      news: [],
      newInfo: {},
      recommendLesson: [],
      sysName: '',
      schoolId: '',
      schoolLogUrl: '',
      dialogVisible: false
    }
  },
  mounted () {
    this.getMain()
  },
  methods: {
    async getMain () {
      let res
      if (this.userInfo.userType === '1') {
        res = await teacherMain({
          systemCode: 'INT_EDU'
        })
      } else {
        res = await studentMain({
          systemCode: 'INT_EDU'
        })
      }
      if (res.code === '200') {
        this.banner = res.data.banner
        this.news = res.data.news
        this.recommendLesson = res.data.recommendLesson
        this.sysName = res.data.sysName
        this.schoolId = res.data.schoolId
        this.schoolLogUrl = res.data.schoolLogUrl
      } else {
        this.$message.error(res.message)
      }
    },
    showDetail (item) {
      this.newInfo = item
      this.dialogVisible = true
    },
    toCourse () {
      if (this.userInfo.userType === '2') {
        this.$router.push('/student/course/index')
      }
    },
    handleBanner (item) {
      if (item.type === '新闻') {
        this.newInfo = item
        this.dialogVisible = true
      }
    }
  }
}
</script>

<style scoped lang="scss">
.banner {
  height: 460px;
  ::v-deep.el-carousel__container {
    height: 460px;
  }
  img {
    width: 100%;
    height: 100%;
  }
}
.active {
  background: rgba(7, 118, 182, 1);
}
.main {
  width: 1200px;
  margin: 0 auto;
  margin-top: 30px;
  .title {
    position: relative;
    display: flex;
    justify-content: space-between;
    .title-left {
      font-weight: 500;
      font-size: 24px;
      color: rgba(16, 29, 55, 1);
      margin-left: 17px;
    }
    .title-right {
      font-size: 14px;
      font-weight: 500;
      color: rgba(16, 29, 55, 1);
      opacity: 0.6;
      line-height: 27px;
      cursor: pointer;
    }
    .title-right:hover {
      color: rgba(16, 29, 55, 1);
      opacity: 0.3;
    }
  }
  .title ::after {
    content: " ";
    position: absolute;
    left: 0;
    top: 3px;
    width: 6px;
    height: 24px;
    background: rgba(0, 156, 246, 1);
    border-radius: 3px;
  }
  .main-left {
    width: 852px;
    float: left;
    .content {
      display: flex;
      flex-wrap: wrap;
      margin-top: 10px;
      .item {
        margin-right: 12px;
        width: 270px;
        height: 287px;
        border: 1px solid #e9e9e9;
        margin-top: 20px;
        cursor: pointer;
        img {
          width: 270px;
          height: 207px;
          display: block;
        }
        .text {
          padding: 10px 20px 20px 14px;
          .text-p1 {
            font-size: 18px;
            font-weight: bold;
            color: rgba(16, 29, 55, 1);
          }
          .text-p2 {
            font-size: 14px;
            font-weight: 500;
            color: rgba(145, 149, 154, 1);
            margin-top: 10px;
            span {
              float: right;
            }
          }
        }
      }
    }
  }
  .main-right {
    float: right;
    width: 318px;
    ul {
      list-style: none;
      margin-top: 30px;
      li {
        font-size: 16px;
        font-weight: 500;
        color: rgba(16, 29, 55, 1);
        line-height: 24px;
        margin-bottom: 20px;
        overflow: hidden;
        .name {
          font-size: 16px;
          font-weight: 500;
          color: #5a74a6;
          line-height: 24px;
          float: left;
          cursor: pointer;
          span {
            margin-left: 10px;
          }
        }
        .time {
          color: #91959a;
        }
        .text {
          float: left;
          width: 250px;
          overflow: hidden;
          height: 50px;
          margin-left: 10px;
          cursor: pointer;
        }
      }
    }
  }
}

.news-box {
  width: 100%;
  .title {
    font-size: 24px;
    margin-bottom: 20px;
    color: #000;
    line-height: 30px;
  }
  .news-desc {
    display: flex;
    justify-content: space-between;
    margin-bottom: 20px;
    align-items: center;
  }
  .news-content {
    font-size: 20px;
    line-height: 30px;
  }
  img {
    width: 100%;
    margin-top: 20px;
  }
}

</style>
